﻿'use strict';

angular.module('app')
    .controller('AppCtrl', [
        '$rootScope', '$localStorage', '$state', '$timeout',
        function ($rootScope, $localStorage, $state, $timeout) {
            $rootScope.settings = {
                skin: '',
                color: {
                    themeprimary: '#2dc3e8',
                    themesecondary: '#fb6e52',
                    themethirdcolor: '#ffce55',
                    themefourthcolor: '#a0d468',
                    themefifthcolor: '#e75b8d'
                },
                rtl: false,
                fixed: {
                    navbar: false,
                    sidebar: false,
                    breadcrumbs: false,
                    header: false
                }
            };
            // 商铺信息
            $rootScope.shop_set = {
                "name":"如果咖啡厅",
                "img":"assets/images/ifImage/logo.png",
                "address":"北京市朝阳区定福庄东街1号",
                "tele_phone":"010-12345678",
                "num":10,
                "id":"123456"
            };

            // 购物车列表
            $rootScope.shopList=[  
               // {
               //      "name":"小炒肉",
               //      "img":"assets/images/img-1.jpg",
               //      "price":7.79,
               //      "number":1
               // },
               // {
               //      "name":"Special Coffee",
               //      "img":"assets/images/img-2.jpg",
               //      "price":7.79,
               //      "number":2
               // } 
            ];

            //添加购物车  
            $rootScope.addShop=function(shop){
                var a = "null";
                console.log(shop.name);

                // 判断购物车中是否已经添加
                for (var i = 0; i < $rootScope.shopList.length; i++) {
                    // debugger;
                    if ($rootScope.shopList[i].name == shop.name) {
                        a = i;
                        console.log(a);
                        // debugger;
                        break;
                    }
                }

                if (a >= 0) { //购物车中已添加
                    // debugger;
                    $rootScope.shopList[a].number++;
                    console.log($rootScope.shopList);
                }
                else {//购物车中还没有该商品
                    // debugger;
                    $rootScope.shopList.push(shop);
                    $rootScope.shopList[$rootScope.shopList.length-1].number = 1;
                    console.log($rootScope.shopList);
                }
            };

            //减少购物车  
            $rootScope.reduceShop=function(shop){
                console.log(shop);
                var a = "null";
                for (var i = 0; i < $rootScope.shopList.length; i++) {
                    if ($rootScope.shopList[i].name == shop.name) {
                        a = i;
                        console.log(a);
                        // debugger;
                        break;
                    }
                }
                if (a!="null") {
                    if($rootScope.shopList[a].number>1){  
                        $rootScope.shopList[a].number--;  
                    }else{  
                        $rootScope.removeShop(a);  
                    }
                }else{
                    alert("无此商品！");
                }
                 
            };


            // 购物车移除一项
            $rootScope.removeShop=function(index){  
                if(confirm('确定移除此项吗？')){  
                    $rootScope.shopList.splice(index,1);  
                }
                 
            };

            //清空购物车
            $rootScope.removeAll=function(){  
                if(confirm('确定清空购物车')){  
                    $rootScope.shopList = [];  
                }  
            };

            

            // console.log($rootScope.shop_set);
            if (angular.isDefined($localStorage.settings))
                $rootScope.settings = $localStorage.settings;
            else
                $localStorage.settings = $rootScope.settings;

            $rootScope.$watch('settings', function () {
                if ($rootScope.settings.fixed.header) {
                    $rootScope.settings.fixed.navbar = true;
                    $rootScope.settings.fixed.sidebar = true;
                    $rootScope.settings.fixed.breadcrumbs = true;
                }
                if ($rootScope.settings.fixed.breadcrumbs) {
                    $rootScope.settings.fixed.navbar = true;
                    $rootScope.settings.fixed.sidebar = true;
                }
                if ($rootScope.settings.fixed.sidebar) {
                    $rootScope.settings.fixed.navbar = true;


                    //Slim Scrolling for Sidebar Menu in fix state
                    var position = $rootScope.settings.rtl ? 'right' : 'left';
                    if (!$('.page-sidebar').hasClass('menu-compact')) {
                        $('.sidebar-menu').slimscroll({
                            position: position,
                            size: '3px',
                            color: $rootScope.settings.color.themeprimary,
                            height: $(window).height() - 90,
                        });
                    }
                } else {
                    if ($(".sidebar-menu").closest("div").hasClass("slimScrollDiv")) {
                        $(".sidebar-menu").slimScroll({ destroy: true });
                        $(".sidebar-menu").attr('style', '');
                    }
                }

                $localStorage.settings = $rootScope.settings;
            }, true);

            $rootScope.$watch('settings.rtl', function () {
                if ($state.current.name != "persian.dashboard" && $state.current.name != "arabic.dashboard") {
                    switchClasses("pull-right", "pull-left");
                    switchClasses("databox-right", "databox-left");
                    switchClasses("item-right", "item-left");
                }

                $localStorage.settings = $rootScope.settings;
            }, true);

            $rootScope.$on('$viewContentLoaded',
                function (event, toState, toParams, fromState, fromParams) {
                    if ($rootScope.settings.rtl && $state.current.name != "persian.dashboard" && $state.current.name != "arabic.dashboard") {
                        switchClasses("pull-right", "pull-left");
                        switchClasses("databox-right", "databox-left");
                        switchClasses("item-right", "item-left");
                    }
                    if ($state.current.name == 'error404') {
                        $('body').addClass('body-404');
                    }
                    if ($state.current.name == 'error500') {
                        $('body').addClass('body-500');
                    }
                    $timeout(function () {
                        if ($rootScope.settings.fixed.sidebar) {
                            //Slim Scrolling for Sidebar Menu in fix state
                            var position = $rootScope.settings.rtl ? 'right' : 'left';
                            if (!$('.page-sidebar').hasClass('menu-compact')) {
                                $('.sidebar-menu').slimscroll({
                                    position: position,
                                    size: '3px',
                                    color: $rootScope.settings.color.themeprimary,
                                    height: $(window).height() - 90,
                                });
                            }
                        } else {
                            if ($(".sidebar-menu").closest("div").hasClass("slimScrollDiv")) {
                                $(".sidebar-menu").slimScroll({ destroy: true });
                                $(".sidebar-menu").attr('style', '');
                            }
                        }
                    }, 500);

                    window.scrollTo(0, 0);
                });
        }
    ]);